/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package zooi;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

/**
*
* @author joopblankert
*/
public class BabbelServer {

   final int POORT = 4444;
   ServerSocket server;
   Socket client;

   public BabbelServer() {
       koppelaanpoort();
       multiclients();
   }

   void multiclients() {
       while (true) {
           try {
               client = server.accept();
               String adres1 = client.getInetAddress().toString();
               String adres2 = client.getRemoteSocketAddress().toString();
               loggen("MULTISERVER: nieuwe client " + adres1 + " geaccepteerd");

               Babbelaar bla = new Babbelaar(client);
               babbelaars.add(bla);
               new Thread(bla).start();

           } catch (Exception e) {
               loggen("MULTISERVER: catch 1 = " + e);
               System.exit(0);
           }
       }
   }

   public static void main(String[] args) {
       new BabbelServer();
   }

   void koppelaanpoort() {
       try {
           server = new ServerSocket(POORT);
           loggen("MULTISERVER: gestart");
       } catch (Exception e) {
           loggen("MULTISERVER: poort niet beschikbaar, server geannuleerd");
           System.exit(0);
       }
   }

   void loggen(String tekst) {
       System.out.println(tekst);
   }
   ArrayList babbelaars = new ArrayList();
   public class Babbelaar implements Runnable {

       BufferedReader lees;
       String tekst, ontvangen;
       PrintWriter schrijf;
       Socket client;
       String user = "";
       public Babbelaar(Socket s) {
           client = s;
       }

       public void run() {
           maakvariabelen();
           communiceerclient();
       }

       void maakvariabelen() {
           try {
               schrijf = new PrintWriter(client.getOutputStream(), true);
               lees = new BufferedReader(new InputStreamReader(client.getInputStream()));
           } catch (Exception e) {
               loggen("MULTISERVER: catch 2 = " + e);
           }
       }

       void communiceerclient() {
           zend("Login naam >: ");
           ontvangen = ontvang();
           user = ontvangen;
           zend("Welkom in Babbelaar "+  user + " spreek >:");

           loggen("MULTISERVER: start communicatie");
           while (!client.isClosed()) {
               ontvangen = ontvang();
               broadcast( "\"" + ontvangen+"\" reageer >: ");
           }
       }

       void zend(String tekst) {
           schrijf.println(tekst);
           loggen("MULTISERVER: verzenden = " + tekst);
       }

       String ontvang() {
           String tekst = "";
           try {
               tekst = lees.readLine();
               if (tekst.equals("XXX")) {
                   loggen("MULTISERVER: client gestopt");
                   client.close();
               } else {
                   loggen("MULTISERVER: ontvangen = " + tekst + " [" + tekst.length() + "]");
               }
           } catch (Exception e) {
               loggen("MULTISERVER: catch 30 " + e);
           }
           return tekst;
       }

      void broadcast(String mes){
           for (int i=0;i<babbelaars.size();i++){
               Babbelaar b = (Babbelaar)babbelaars.get(i);

               b.zend(user + " zegt " + mes);

           }
       }
   }
}